import axios from 'axios'
/*知识库操作 */
const base = axios.create({
  baseURL: '/knowledge_base'
})
// 添加请求拦截器
// Chat.interceptors.request.use(
//   function (config) {
//     return config
//   },
//   function (error) {
//     // 对请求错误做些什么
//     return Promise.reject(error)
//   }
// )
// 添加响应拦截器
base.interceptors.response.use(
  function (res) {
    if (res.data.code === 200) {
      console.log(JSON.stringify(res.data))
      return res
    }
    // 处理业务失败, 给错误提示，抛出错误
    ElMessage.error(res.data.msg || '服务异常')
    console.log(JSON.stringify(res.data))
    return Promise.reject(res.data)
  },
  function (error) {
    return Promise.reject(error)
  }
)

// 创建知识库
export const createKnowledgeBaseService = (base_name) =>
  base.post('/create_knowledge_base', {
    knowledge_base_name: base_name,
    vector_store_type: 'fChatss',
    embed_model: 'bge-large-zh'
  })

//获取知识库列表
export const getBaseListService = () => base.get('/list_knowledge_bases')

//上传文件到知识库
export const uploadDocsService = (data) => {
  const config = {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  }
  return base.post('/upload_docs', data, config) // 将配置对象作为第三个参数传递
}
//获取知识库内的文件列表
export const getDocsListService = (base_name) =>
  base.get('/list_files', {
    params: {
      knowledge_base_name: base_name
    }
  })
//删除知识库
export const deleteBaseService = (base_name) =>
  base.post('/delete_knowledge_base', base_name)
//删除知识库中的文件
export const deleteDocsService = (base_name, files) =>
  base.post('/delete_docs', {
    knowledge_base_name: base_name,
    file_names: files,
    delete_content: true,
    not_refresh_vs_cache: true
  })

// 与知识库对话
export const baseChatService = (query, base_name) =>
  axios.post('/chat/knowledge_base_chat', {
    query: query,
    knowledge_base_name: base_name,
    top_k: 10,
    score_threshold: 1.2,
    // history: history,
    history: [
      {
        role: 'user',
        content:
          '根据知识库内容回答我的问题，请回答你所知道的全部信息,禁止回答知识库中不存在的信息,保持回答的简洁'
      },
      {
        role: 'assistant',
        content: '好的，请提问'
      }
    ],
    stream: false,
    model_name: 'zhipu-api',
    temperature: 0.9,
    max_tokens: 0,
    prompt_name: 'default'
  })
// 与Chat模型对话
export const ChatService = (query) =>
  axios.post('/chat/chat', {
    query: query,
    search_engine_name: 'duckduckgo',
    top_k: 3,
    stream: false,
    model_name: 'zhipu-api',
    temperature: 0.7,
    max_tokens: 0,
    prompt_name: 'default',
    split_result: false
  })
